package com.nefu.editor.service.Impl;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.core.mapper.ArticleMapper;
import com.nefu.editor.core.mapper.UserMapper;
import com.nefu.editor.core.model.Article;
import com.nefu.editor.core.model.User;
import com.nefu.editor.core.model.Vo.ArticleVo;
import com.nefu.editor.service.ArticleService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenchenT
 * @date : 18-11-14
 * @since : java 8
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    private final ArticleMapper articleMapper;
    private final UserMapper userMapper;

    public ArticleServiceImpl(ArticleMapper articleMapper, UserMapper userMapper) {
        this.articleMapper = articleMapper;
        this.userMapper = userMapper;
    }

    @Override
    public List<Map<String, Object>> getArticleListByKeyWord(ArticleVo articleVo) throws EdiException {
        List<Map<String, Object>> rtv = new ArrayList<>();
        if (null != articleVo) {
            int requestCount = 0;
            if (null != articleVo.getRequestCount()) {
                requestCount = articleVo.getRequestCount();
            }
            articleVo.setL(requestCount * 2);
            articleVo.setOffset(2);
            List<Article> articles = articleMapper.selectLatestArticle(articleVo);
            if (null != articles && 0 != articles.size()) {
                for (Article article : articles) {
                    Map<String, Object> map = new HashMap<>(8);
                    map.put("systemId", article.getSystemId());
                    map.put("title", article.getTitle());
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    map.put("time", formatter.format(article.getTime()));
                    map.put("summary", article.getSummary());
                    map.put("image", article.getImage());
                    map.put("likesNumber", article.getLikesNumber());
                    map.put("collectsNumber", article.getCollectsNumber());
                    if (null != article.getWriterId()) {
                        User user = new User();
                        user.setSystemId(article.getWriterId());
                        List<User> users = userMapper.selectUserByCondition(user);
                        if (null != users && 1 == users.size()) {
                            map.put("writer", users.get(0).getIdNumber());
                        }
                    }
                    rtv.add(map);
                }
                return rtv;
            } else {
                throw new EdiException("数据库中已经没有该类型的文章啦!");
            }
        } else {
            throw new EdiException("参数有误!");
        }
    }

    @Override
    public Map<String, Object> getArticleDetailByArticleSystemId(Article article) throws EdiException {
        Map<String, Object> map = new HashMap<>(8);
        if (null != article && null != article.getSystemId()) {
            List<Article> articles = articleMapper.selectArticleByCondition(article);
            if (null != articles && 1 == articles.size()) {
                article = articles.get(0);
                map.put("systemId", article.getSystemId());
                map.put("title", article.getTitle());
                if (null != article.getWriterId()) {
                    User user = new User();
                    user.setSystemId(article.getWriterId());
                    List<User> users = userMapper.selectUserByCondition(user);
                    if (null != users && 1 == users.size()) {
                        map.put("writer", users.get(0).getIdNumber());
                    }
                    user.setSystemId(article.getThinkerId());
                    users = userMapper.selectUserByCondition(user);
                    if (null != users && 1 == users.size()) {
                        map.put("thinker", users.get(0).getIdNumber());
                    }
                }
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                map.put("time", formatter.format(article.getTime()));
                map.put("content", article.getContent());
                map.put("image", article.getImage());
            }
        } else {
            throw new EdiException("参数有误!");
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> getArticleByWriterId(User user) throws EdiException {
        List<Map<String, Object>> rtv = new ArrayList<>();
        if (null != user && null != user.getSystemId()) {
            Article article = new Article();
            article.setWriterId(user.getSystemId());
            List<Article> articles = articleMapper.selectArticleByCondition(article);
            for (Article article1 : articles) {
                Map<String, Object> map = new HashMap<>(8);
                map.put("systemId", article1.getSystemId());
                map.put("title", article1.getTitle());
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                map.put("time", formatter.format(article1.getTime()));
                map.put("summary", article1.getSummary());
                map.put("image", article1.getImage());
                map.put("likesNumber", article1.getLikesNumber());
                map.put("collectsNumber", article1.getCollectsNumber());
                List<User> users = userMapper.selectUserByCondition(user);
                if (null != users && 1 == users.size()) {
                    map.put("writer", users.get(0).getIdNumber());
                }
                rtv.add(map);
            }
        } else {
            throw new EdiException("参数有误!");
        }
        return rtv;
    }

}
